<script type="text/javascript">
var enable_submit = true;

function submit_pool_update() {
    var bValid = true;
    var modal_selector = $("#pool_update_modal");
    var protection_domain = modal_selector.find(".pool_PDs");
    var repnum = modal_selector.find("#repnum");
    var target_tier = modal_selector.find("#pool_tt_select");
    var quota = modal_selector.find("#quota");
    var chap_name = modal_selector.find("#chap_name");
    var chap_password = modal_selector.find("#chap_password");
    var chap_password= modal_selector.find("#chap_password");
    var delete_code = $("#pool_update_modal").find("#delete_code");
    var delete_code_r = $("#pool_update_modal").find("#delete_code_r");
    var provisioning = $('#pool_update_modal').find("input[name='disk_preparation']:checked").val();
    if($.trim(quota.val()).length>0)   {                 
        bValid = bValid && checkRegexp_num(quota, /^[0-9]+$/i, "配额必须为大于0的整数");
    }
    if($("#rep_op").is(":hidden")){
        repnum.attr("realvalue","");
    }
    if($("#code_delete_op").is(":hidden")){
        delete_code.attr("realvalue","");
        delete_code_r.attr("realvalue","");
    }

    if ($.trim(chap_name.val()).length > 0 || $.trim(chap_password.val()).length > 0) {
        bValid = bValid && checkCHAP(chap_name, chap_password);
    }
    PD_ids = _multi_checkbox_get_ids(protection_domain);

    if (bValid) {
        if (enable_submit) {
            enable_submit = false;
            var $submitBtn = $('.modal-footer').find('.btn-blue');
            $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
            $('.rtn-tip').slideUp('fast');
            $.post("/pool/update", {
                "quota": $.trim(quota.val()),
                "id": "{{pool.id}}",
                "protection_domain_ids": PD_ids,
                "repnum": $.trim(repnum.attr("realvalue")),
                "priority": $.trim(target_tier.attr("realvalue")),
                'chap_name': $.trim(chap_name.val()),
                'chap_password': $.trim(chap_password.val()),
                "ec_code": $.trim(delete_code_r.attr("realvalue")),
                "ec_data": $.trim(delete_code.attr("realvalue")),
                "provisioning":provisioning
            }, function(data) {
                if (data.reply.is_success) {
                    angular.element('#pool_create_button').scope().close();
                    $("#pool_available_tb").trigger("reloadGrid");
                    //form_validation = true;
                } else {
                    error = data.reply.error;
                    $('.rtn-tip').text(error).slideDown('fast');
                    $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
                    enable_submit = true;
                }
            });
        }
    }
}

$(function() {

    if ('{{ pool.protocol }}' == 'nbd' || '{{ pool.protocol }}' == 'nfs') {
        $('.user-info').hide();
    }

    $('#pool_update_modal #repnum').rqDropDown({
        "datatype": "local",
        "source": get_repnums(),
        position: true
    });

    $('#pool_update_modal #pool_tt_select').rqDropDown({
        "datatype": "local",
        "source": get_tiers(),
        position: true
    });
    var delete_code = [['2', '2'],['3', '3'],['4', '4'],['6', '6'],['8', '8'],['12', '12'],['16', '16']];
    $('#pool_update_modal #delete_code').rqDropDown({
        "datatype":"local",
        "source":delete_code
    });
    var delete_code_r = [['1', '1'],['2', '2'],['3', '3'],['4', '4']];
    $('#pool_update_modal #delete_code_r').rqDropDown({
        "datatype":"local",
        "source":delete_code_r
    });

    var obj = {
        'quota': '不填写，则配额不受限，若填写，须为大于0的整数。',
        'chap_password': '可为空, 为空时将使用所属资源池的CHAP用户名称;由3至32位小写英文字母、数字、中划线构成。',
        'chap_name': '可为空, 为空时将使用所属资源池的CHAP用户名称;如果资源池未设置CHAP用户，则使用所属用户的CHAP用户;由3至32位小写英文字母、数字、中划线构成'
    }
    bindMouseEvent('#pool_update_modal', obj);
    /* 为小图标绑定鼠标移入移出事件结束 */
});
</script>
<div class="modal-header">
    <h3>编辑资源池</h3>
</div>
<div class="modal-body modal-body-height" id="pool_update_modal">
    <div class="pool-update-box">
        <div class="rtn-tip"></div>
        
        <table class="base-info">
            <tr>
                <td> </td>
                <td colspan="3"></td>
            </tr>
            <tr>
                <td>
                    <label>[['model storage capacity'|translate]]</label>
                </td>
                <td>
                    <input type="text" name="quota" id="quota" value="{{ pool.quota|default('', True)}}">
                    <span class="unit">G</span>
                    <i class="fa fa-question-circle" itype="quota"></i>
                </td>
                <td></td>
                <td></td>
            </tr>
        </table>
        <table class="super-info">
            <tr>
                <td>
                    <label>保护域</label>
                </td>
                <td>
                    {% for pd in protection_domains %} {% if pd.id in pool_pd_ids %}
                    <label>
                        <input type="checkbox" checked class="pool_PDs" value="{{pd.id}}">{{pd.name}}</label>
                    {% else %}
                    <label>
                        <input type="checkbox" class="pool_PDs" value="{{pd.id}}">{{pd.name}}</label>
                    {% endif %} {% endfor %}
                </td>
                <td>
                    <label>[['replication number'|translate]]</label>
                </td>
                <td>
                        {% if pool.ec_data %}
                    <input id="" style="width:40%;" readonly="readonly" class="rounded_dropdown" value='{{pool.ec_data}}' realvalue="{{pool.ec_data}}"/>+
                    <input  id=""  style="width:40%;" readonly="readonly" class="rounded_dropdown" value='{{pool.ec_code}}' realvalue="{{pool.ec_data}}"/>

                            {% else %}
                            <input id="repnum" readonly="readonly" class="rounded_dropdown" value='{{pool.repnum}}' realvalue="{{pool.repnum}}"/>
                        {% endif %}
            </td>     
        </tr>
        <tr>
            <td>
                <label>[['Disk configuration'|translate]]</label><a>*</a>
            </td>
            <td>
                {% if provisioning == 'thin' %}
            <label><input  type="radio" name="disk_preparation" value="thin" style="vertical-align:-3px;" checked=true/>[['modal complex'|translate]]</label>
                    <label><input  type="radio" name="disk_preparation" value="thick" style="vertical-align:-3px;"/>[['modal streamline'|translate]]</label>
                {% else %}
                    <label><input  type="radio" name="disk_preparation" value="thin" style="vertical-align:-3px;" />[['modal complex'|translate]]</label>    
                    <label><input  type="radio" name="disk_preparation" value="thick" style="vertical-align:-3px;" checked=true/>[['modal streamline'|translate]]</label>
                {% endif %}
            </td>
        </tr>
            <tr>
                <td>
                    <label>目标分层</label>
                </td>
                <td>
                    <input id="pool_tt_select" readonly="readonly" class="rounded_dropdown" value="[['{{pool.priority}}'|translate]]" realvalue="{{pool.priority}}" />
                </td>
                <td></td>
                <td></td>
            </tr>
        </table>
        <table class="user-info">
            <tr>
                <td>
                    <label>[["label chap name"|translate]]</label>
                </td>
                <td>
                    <input type="text" name="chap_name" id="chap_name" value="{{ pool.sp_chap_name|default('',True)}}">
                    <i class="fa fa-question-circle" itype="chap_name"></i>
                </td>
                <td>
                    <label>[["label chap password"|translate]]</label>
                </td>
                <td>
                    <input type="password" name="chap_password" id="chap_password" value="{{ pool.sp_chap_password|default('',True)}}" readonly onfocus="this.removeAttribute('readonly');" onpaste="return false" oncontextmenu="window.event.returnValue=false">
                    <i class="fa fa-eye" style="right:60px"></i>
                    <i class="fa fa-question-circle" itype="chap_password"></i>
                </td>
            </tr>
        </table>
    </div>
    <div class="tip">
        <p></p>
        <span></span>
    </div>
    <div class="error-tip">
        <p></p><span></span>
    </div>
</div>
<div class="modal-footer">
    <button class="btn btn-blue" ng-click="update('{{ pool.id }}')">[[ 'label ok'|translate ]]</button>
    <button class="btn btn-white" ng-click="cancel()">[[ 'label cancel'|translate ]]</button>
</div>
